import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import {
  addVideo,
  deleteVideo,
  getDetailVideoInfo,
  getVideoList,
  modifyVideo,
} from "../../service/modules/video";

interface IVideoState {
  videoList: any[];
  total: number;
  videoDetail: any;
}
const initialState: IVideoState = {
  videoList: [],
  total: 0,
  videoDetail: {},
};
/**
 * @getlist
 */
export const fetchGetVedioAction = createAsyncThunk(
  "video/list",
  async (query: { pageNum: number; pageSize: number }) => {
    const res = await getVideoList(query);
    console.log(res);
    return res;
  }
);
export const fetchVideoDetailInfo = createAsyncThunk(
  "video/detail",
  async (id: number) => {
    const res = await getDetailVideoInfo(id);
    return res.data;
  }
);
export const fetchUpdateVideoAction = createAsyncThunk(
  "video/upload",
  async (body: {
    createBy: string | null;
    createTime: string | null;
    department: string;
    fileName: string | null;
    id: number | null;
    remark: null;
    state: string;
    sysDeptId: null;
    title: string;
    updateBy: string;
    updateTime: string | null;
    userId: null;
    video: null | string;
  }) => {
    const res = await modifyVideo(body);
    console.log(res);
    return res;
  }
);

export const fetchAddVideoAction = createAsyncThunk(
  "video/add",
  async (body: {
    createBy: null | string;
    createTime: string | null;
    department: string;
    fileName: string | null;
    id: null | number;
    remark: null;
    state: string;
    sysDeptId: null | number;
    title: string;
    updateBy: null;
    updateTime: null;
    video: string | null;
  }) => {
    const res = await addVideo(body);
    console.log(res);
    return res;
  }
);

export const fetchDeleteVideoAction = createAsyncThunk(
  "video/delete",
  async (ids: any[]) => {
    const res = await deleteVideo(ids);
    console.log(res);
    return res;
  }
);

const VideoSlice = createSlice({
  name: "video",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchGetVedioAction.fulfilled, (state, action) => {
        state.videoList = action.payload.rows;
        state.total = action.payload.total;
      })
      .addCase(fetchVideoDetailInfo.fulfilled, (state, action) => {
        state.videoDetail = action.payload;
      });
  },
});

export default VideoSlice.reducer;
